package com.xn.app.muying.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xn.app.muying.R;
import com.xn.app.muying.view.PickerView;

public class MyAlertDialog {
    private static AlertDialog ad;
    private static AlertDialog out;
    private static AlertDialog photo;
    //标题
    private TextView serviceCode = null;
    //内容
    private TextView data = null;
    //更新栏关闭按钮
    private ImageView close = null;
    //取消
    private TextView cancel_text = null;
    //确定
    private TextView confirm_text = null;
    //进度条
    private ProgressBar mProgress = null;
    //相机
    private ImageView image1 = null;
    //图库
    private ImageView image2 = null;
    //显示头像的gridview
    private GridView gridview = null;
    //日期设置控件
    private PickerView date_picker1 = null;
    private TextView text1= null;
    //日期设置控件
    private PickerView date_picker2 = null;
    private TextView text2= null;
    //日期设置控件
    private PickerView date_picker3 = null;
    private TextView text3= null;


    /**
     * 加载进度
     *
     * @param activity
     * @param res
     * @return
     */
    public AlertDialog ShowDialog(final Activity activity, int res) {
        ad = new AlertDialog.Builder(activity, R.style.Theme_Transparent).create();
        ad.setCancelable(true);
        ad.show();
        Window window = ad.getWindow();
        ad.getWindow().setContentView(res);
        //获取一个LayoutParams对象用于后面设置回去
        assert window != null;
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.0f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        ad.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                    if (ad != null) {
                        ad.dismiss();
                    }
                    if (out != null) {
                        out.dismiss();
                    }
                    if (photo != null) {
                        photo.dismiss();
                    }
                    activity.finish();
                    return true;
                }
                return false;
            }
        });
        return ad;
    }

    /**
     * 加载进度，无返回关闭事件
     *
     * @param activity
     * @param res
     * @return
     */
    public AlertDialog ShowDialogWeb(final Activity activity, int res) {
        ad = new AlertDialog.Builder(activity, R.style.Theme_Transparent).create();
        ad.setCancelable(true);
        ad.show();
        Window window = ad.getWindow();
        ad.getWindow().setContentView(res);
        //获取一个LayoutParams对象用于后面设置回去
        assert window != null;
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.0f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        return ad;
    }

    /**
     * 保存胎心弹出框
     *
     * @param activity
     * @param res
     * @return
     */
    public static AlertDialog SaveDialog(final Activity activity, int res) {
        ad = new AlertDialog.Builder(activity, R.style.Theme_Transparent).create();
        ad.setCancelable(false);
        ad.show();
        Window window = ad.getWindow();
        ad.getWindow().setContentView(res);
        //获取一个LayoutParams对象用于后面设置回去
        assert window != null;
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.0f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        return ad;
    }

    /**
     * 宝宝是否出生弹窗
     *
     * @param context
     * @param layout
     * @return
     */
    public AlertDialog Prompt_Dialog(Context context, int layout) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(true);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        data = (TextView) window.findViewById(R.id.data);
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        return ad;
    }

    /**
     * 体温监测提示弹窗
     *
     * @param context
     * @param layout
     */
    public AlertDialog Temperature_Dialog(Context context, int layout) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(false);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //获取一个LayoutParams对象用于后面设置回去
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.0f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //得到控件
        data = (TextView) window.findViewById(R.id.data);
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        return ad;
    }

    /**
     * 账号过期，重新登录
     *
     * @param context
     * @param layout
     * @return
     */
    public AlertDialog DengLu_Dialog(Context context, int layout) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(false);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        data = (TextView) window.findViewById(R.id.data);
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        return ad;
    }

    /**
     * 设置提醒时间
     *
     * @param context
     * @param layout
     * @return
     */
    public AlertDialog SetTime_Dialog(Context context, int layout) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(true);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        date_picker1 = (PickerView) window.findViewById(R.id.date_picker1);
        date_picker2 = (PickerView) window.findViewById(R.id.date_picker2);
        date_picker3 = (PickerView) window.findViewById(R.id.date_picker3);
        text1= (TextView) window.findViewById(R.id.text1);
        text2= (TextView) window.findViewById(R.id.text2);
        text3= (TextView) window.findViewById(R.id.text3);
        return ad;
    }

    /**
     * 预约规则弹窗
     *
     * @param context
     * @param layout
     * @return
     */
    public AlertDialog rule_Dialog(Context context, int layout) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(true);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        data = (TextView) window.findViewById(R.id.data);
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        return ad;
    }

    /**
     * APP更新弹窗
     *
     * @param context
     * @param layout
     */
    public AlertDialog upDate_Dialog(Context context, int layout, boolean lean) {
        out = new AlertDialog.Builder(context, R.style.dialog).create();
        out.setCancelable(lean);
        out.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = out.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        serviceCode = (TextView) window.findViewById(R.id.serviceCode);
        data = (TextView) window.findViewById(R.id.data);
        close = (ImageView) window.findViewById(R.id.close);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        return out;
    }

    /**
     * 更新进度条
     *
     * @return
     */
    public AlertDialog upDate_item_Dialog(Context context, int layout) {
        out = new AlertDialog.Builder(context, R.style.dialog).create();
        out.setCancelable(false);
        out.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = out.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        data = (TextView) window.findViewById(R.id.data);
        mProgress = (ProgressBar) window.findViewById(R.id.progress);
        return out;
    }

    /**
     * 会员判断弹窗
     *
     * @param context
     * @param layout
     */
    public AlertDialog vip_Dialog(Context context, int layout) {
        out = new AlertDialog.Builder(context, R.style.dialog).create();
        out.setCancelable(true);
        out.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = out.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        serviceCode = (TextView) window.findViewById(R.id.serviceCode);
        data = (TextView) window.findViewById(R.id.data);
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        return out;
    }

    /**
     * 拍照或者相册
     *
     * @param context
     * @param layout
     * @return
     */
    public AlertDialog PhotoGraph_Dialog(Context context, int layout) {
        photo = new AlertDialog.Builder(context, R.style.dialog).create();
        photo.setCancelable(true);
        photo.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = photo.getWindow();
        assert window != null;
        window.setContentView(layout);
        //获取一个LayoutParams对象用于后面设置回去
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.2f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //得到控件
        image1 = (ImageView) window.findViewById(R.id.image1);
        image2 = (ImageView) window.findViewById(R.id.image2);
        data = (TextView) window.findViewById(R.id.data);
        return photo;
    }

    /**
     * 头像选择页面
     *
     * @param context
     * @param layout
     */
    public AlertDialog Head_Dialog(Context context, int layout) {
        ad = new AlertDialog.Builder(context, R.style.dialog).create();
        ad.setCancelable(true);
        ad.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        assert window != null;
        window.setContentView(layout);
        //获取一个LayoutParams对象用于后面设置回去
        WindowManager.LayoutParams lp = window.getAttributes();
        //调整明暗度，float值，完全透明不变暗是0.0f，完全变暗不透明是1.0f
        lp.dimAmount = 0.2f;
        //必须要设置回去
        window.setAttributes(lp);
        //根据谷歌文档，给对应的Window添加FLAG_DIM_BEHIND标志位，dimAmount值才有效。
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        //得到控件
        gridview = (GridView) window.findViewById(R.id.gridview);
        return ad;
    }

    /**
     * 支付成功监测首页弹框
     *
     * @param context
     * @param layout
     */
    public AlertDialog Monitor_Dialog(Context context, int layout) {
        out = new AlertDialog.Builder(context, R.style.dialog).create();
        out.setCancelable(true);
        out.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = out.getWindow();
        assert window != null;
        window.setContentView(layout);
        //得到控件
        data = (TextView) window.findViewById(R.id.data);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        return out;
    }

    /**
     * 胎心监测蓝牙连接提示框
     *
     * @param context
     * @param layout
     */
    public AlertDialog Bluetooth_Dialog(Context context, int layout) {
        out = new AlertDialog.Builder(context, R.style.dialog).create();
        out.setCancelable(true);
        out.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = out.getWindow();
        assert window != null;
        window.setContentView(layout);
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        return out;
    }

    /**
     * 胎心监测怀孕状态判断
     *
     * @param context
     * @param layout
     */
    public AlertDialog PregnancyStatus_Dialog(Context context, int layout) {
        out = new AlertDialog.Builder(context, R.style.dialog).create();
        out.setCancelable(true);
        out.show();
        // 关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = out.getWindow();
        assert window != null;
        window.setContentView(layout);
        data = (TextView) window.findViewById(R.id.data);
        cancel_text = (TextView) window.findViewById(R.id.cancel_text);
        confirm_text = (TextView) window.findViewById(R.id.confirm_text);
        return out;
    }
    //提供标题控件

    public TextView getServiceCode() {
        return serviceCode;
    }

    //提供内容控件
    public TextView getData() {
        return data;
    }

    //提供关闭控件
    public ImageView getClose() {
        return close;
    }


    //提供取消控件
    public TextView getCancel_text() {
        return cancel_text;
    }

    //提供确定控件
    public TextView getConfirm_text() {
        return confirm_text;
    }

    //提供进度条控件
    public ProgressBar getProgressBar() {
        return mProgress;
    }
    //提供拍照控件

    public ImageView getImage1() {
        return image1;
    }
    //提供相册控件

    public ImageView getImage2() {
        return image2;
    }

    //提供时间控件
    public PickerView getDate_picker1() {
        return date_picker1;
    }
    //提供时间控件
    public TextView getText1() {
        return text1;
    }

    //提供时间控件
    public PickerView getDate_picker2() {
        return date_picker2;
    }
    //提供时间控件
    public TextView getText2() {
        return text2;
    }

    //提供时间控件
    public PickerView getDate_picker3() {
        return date_picker3;
    }
    //提供时间控件
    public TextView getText3() {
        return text3;
    }


    //关闭拍照
    public void dismissPhoto() {
        photo.dismiss();
    }

    //关闭
    public void dismissAd() {
        ad.dismiss();
        ad = null;
    }

    //关闭
    public void dismiss() {
        ad.dismiss();
        ad = null;
    }

    //关闭更新
    public void dismissOut() {
        out.dismiss();
    }

    //提供gridview控件

    public GridView getGridView() {
        return gridview;
    }
}
